"""
python fujian1_smooth_gragh.py
"""


import os
import json
import pandas as pd
import matplotlib.pyplot as plt

# 输入和输出路径
group_output_dir = "fujian/fujian1/group_output"
smooth_output_dir = "fujian/fujian1/smooth_preprocess"
graph_output_dir = os.path.join(smooth_output_dir, "gragh")
os.makedirs(graph_output_dir, exist_ok=True)

# 遍历每个类别的 JSON 文件
for filename in os.listdir(group_output_dir):
    if filename.endswith(".json"):
        group_filepath = os.path.join(group_output_dir, filename)
        smooth_filepath = os.path.join(smooth_output_dir, filename)

        # 读取原始数据和预处理数据
        with open(group_filepath, 'r', encoding='utf-8') as file:
            original_data = json.load(file)

        with open(smooth_filepath, 'r', encoding='utf-8') as file:
            smoothed_data = json.load(file)

        # 转换为 DataFrame
        original_df = pd.DataFrame(original_data)
        smoothed_df = pd.DataFrame(smoothed_data)

        # 转换日期格式
        original_df['date'] = pd.to_datetime(original_df['date'])
        smoothed_df['date'] = pd.to_datetime(smoothed_df['date'])

        # 绘制图形
        plt.figure(figsize=(12, 6))
        plt.plot(original_df['date'], original_df['inventory'], label='Original Inventory', marker='o', color='blue')
        plt.plot(smoothed_df['date'], smoothed_df['inventory'], label='Smoothed Inventory', marker='x', color='orange')
        plt.title(f'Inventory Data Comparison - {filename}')
        plt.xlabel('Date')
        plt.ylabel('Inventory')
        plt.xticks(rotation=45)
        plt.legend()
        plt.grid()
        
        # 保存图形
        graph_filepath = os.path.join(graph_output_dir, f'graph_{filename}.png')
        plt.savefig(graph_filepath, bbox_inches='tight')
        plt.close()
        
        print(f"Graph saved: {graph_filepath}")
